﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using NCT.Framework;
using NCT.Framework.Database;
using System.Data.Common;

namespace VISS.API.Users {

    class UserMessageNotificationDA {

        private const string SP_CountUnReadMessagesInDay = "dbo.UserMessage_CountUnReadMessagesInDay";

        public static UserMessageNotificationEnt CreateFromReader(IDataReader reader) {
            UserMessageNotificationEnt item = new UserMessageNotificationEnt();
            item.UserKey = ConvertUtils.ToString(reader["UserKey"]);
            item.Name = ConvertUtils.ToString(reader["Name"]);
            item.Email = ConvertUtils.ToString(reader["Email"]);
            item.MessagesInDay = ConvertUtils.ToInt(reader["MessagesInDay"]);
            return item;
        }

        public static List<UserMessageNotificationEnt> GetByQueryString(DateTime startTime, DateTime endTime, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            List<UserMessageNotificationEnt> result = new List<UserMessageNotificationEnt>();
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_CountUnReadMessagesInDay)) {
                db.AddParameter(cmd, "StartTime", startTime);
                db.AddParameter(cmd, "EndTime", endTime);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    while (reader.Read()) {
                        result.Add(CreateFromReader(reader));
                        //item.MessagesInDay = UserMessageDA.CountUnReadMessagesInDay(item.UserKey, startTime, endTime, connectionName, null, null);
                        //result.Add(item);
                    }
                }
            }
            return result;
        }
    }
}
